package com.ruoyi.web.controller.jjm;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.ruoyi.common.config.Global;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.jjm.domain.*;
import com.ruoyi.jjm.service.IAreaInfoService;
import com.ruoyi.jjm.service.ICountyStatisticsService;
import com.ruoyi.jjm.utils.CustomHandler;
import com.ruoyi.jjm.utils.EasyExcelUtil;
import com.ruoyi.system.domain.SysDept;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.service.ISysDeptService;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/jjm/shi")
public class ShiController extends BaseController {

    private String prefix = "jjm/shi";

    @Autowired
    ICountyStatisticsService countyStatisticsService;

    @Autowired
    IAreaInfoService areaInfoService;
    @Autowired
    private ISysDeptService deptService;

    /**
     * 市级首页
     * @return
     */
    @GetMapping(value = "/info")
    public ModelAndView info() {
        SysUser user = ShiroUtils.getSysUser();
        CountyStatistics chartInfo = countyStatisticsService.selectCountyStatisticsChart();
        Double totalAmount = countyStatisticsService.sumBuyNum();
        ModelAndView mav = new ModelAndView(prefix + "/shi");
        mav.addObject("chartInfo",chartInfo);
        mav.addObject("totalAmount",totalAmount);
        return mav;
    }

    /**
     * 手机端首页
     * @return
     */
    @GetMapping(value = "/mobile")
    public ModelAndView mobile() {
        SysUser user = ShiroUtils.getSysUser();
        Double totalAmount = countyStatisticsService.sumBuyNum();
        ModelAndView mav = new ModelAndView(prefix + "/shiMobile");
        mav.addObject("totalAmount",totalAmount);
        List<SysDept> deptList = deptService.selectDeptByParentId(user.getDeptId());
        mav.addObject("deptList",deptList);
        return mav;
    }

    /**
     * 县信息列表
     * @return
     */
    @GetMapping(value = "/county")
    public ModelAndView county() {
        SysUser user = ShiroUtils.getSysUser();
        CountyStatistics chartInfo = countyStatisticsService.selectCountyStatisticsChart();
        Double totalAmount = countyStatisticsService.sumBuyNum();
        ModelAndView mav = new ModelAndView(prefix + "/info");
        mav.addObject("chartInfo",chartInfo);
        mav.addObject("totalAmount",totalAmount);
        return mav;
    }

    @PostMapping("/shiTableInfo")
    @ResponseBody
    public TableDataInfo shiInfoTable() {
        SysUser user = ShiroUtils.getSysUser();
        startPage();
        List<CountyStatistics> tableList = countyStatisticsService.selectCityStatistics();
        if(CollectionUtils.isNotEmpty(tableList)){

            CountyStatistics countyStatistics = tableList.get(0);
            countyStatistics.setCountyName("承德市");
            Long areaId = user.getDeptId();
            AreaInfo areaInfo = areaInfoService.selectAreaInfoByAreaId(areaId);
            if(areaInfo != null){
                countyStatistics.setCityBlNum(areaInfo.getBaolianCadreNum());
                countyStatistics.setCityBlTownNum(areaInfo.getBaolianAreaNum());
            }
        }
        return getDataTable(tableList);
    }

    @PostMapping("/tableInfo")
    @ResponseBody
    public TableDataInfo infoTable() {
        SysUser user = ShiroUtils.getSysUser();
        startPage();
        List<CountyStatistics> tableList = countyStatisticsService.selectCountyStatisticsList(new CountyStatistics());
        return getDataTable(tableList);
    }


    @PostMapping("/exportShi")
    @ResponseBody
    public AjaxResult exportShi() throws InvocationTargetException, IllegalAccessException {
        SysUser user = ShiroUtils.getSysUser();
        SysDept dept = user.getDept();
        List<CountyStatistics> list = countyStatisticsService.selectCityStatistics();

        if(CollectionUtils.isEmpty(list)){
            return AjaxResult.warn("居民户籍信息为空");
        }
        CountyStatistics countyStatistics = list.get(0);
        countyStatistics.setCountyName("承德市");
        Long areaId = user.getDeptId();
        AreaInfo areaInfo = areaInfoService.selectAreaInfoByAreaId(areaId);
        if(areaInfo != null){
            countyStatistics.setCityBlNum(areaInfo.getBaolianAreaNum());
            countyStatistics.setCityBlTownNum(areaInfo.getBaolianCadreNum());
        }
        List<CityStatisticsTable> tableList = new ArrayList<>();
        CityStatisticsTable table = new CityStatisticsTable();
        BeanUtils.copyProperties(table,countyStatistics);
        tableList.add(table);
        String fileName = "承德市居民户籍信息" + System.currentTimeMillis() + ".xlsx";

        EasyExcel.write(Global.getDownloadPath()+fileName, CityStatisticsTable.class)
                .needHead(true)
                .head(CityStatisticsTable.class)
                .excelType(ExcelTypeEnum.XLSX)
                .registerWriteHandler(new CustomHandler())
                .registerWriteHandler(EasyExcelUtil.getStyleStrategy()).sheet("居民户籍信息").doWrite(tableList);
        return AjaxResult.success(fileName);
    }

    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export() throws InvocationTargetException, IllegalAccessException {
        SysUser user = ShiroUtils.getSysUser();
        SysDept dept = user.getDept();

        List<CountyStatistics> list = countyStatisticsService.selectCountyStatisticsList(new CountyStatistics());
        if(CollectionUtils.isEmpty(list)){
            return AjaxResult.warn("居民户籍信息为空");
        }
        List<CountyStatisticsTable> tableList = new ArrayList<>();
        for(CountyStatistics statistics : list){
            CountyStatisticsTable table = new CountyStatisticsTable();
            BeanUtils.copyProperties(table,statistics);
            tableList.add(table);
        }
        String fileName = "承德市居民户籍信息" + System.currentTimeMillis() + ".xlsx";

        EasyExcel.write(Global.getDownloadPath()+fileName, CountyStatisticsTable.class)
                .needHead(true)
                .head(CountyStatisticsTable.class)
                .excelType(ExcelTypeEnum.XLSX)
                .registerWriteHandler(new CustomHandler())
                .registerWriteHandler(EasyExcelUtil.getStyleStrategy()).sheet("居民户籍信息").doWrite(tableList);
        return AjaxResult.success(fileName);
    }
}
